<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use think\Request;
use think\Session;
use app\admin\model\Judge as mjudge;
class Judge extends Common
{
    public function add()
    {
        if(Request::instance()->isGet()){
            $id = Request::instance()->param('id');
            if($id){
                $choice = mjudge::get($id);
                $this->assign("cdata",$choice);
                return $this->fetch('edit');
            }else{
                return $this->fetch();
            }
        }
    }

    //处理判断题
    public function doAdd(){
    	if (Request::instance()->isPost()) {

    		$data = Request::instance()->post();
            $data['creater'] = Session::get('id');
            if (!isset($data['answer'])) {
                $this->error('请设置答案');
            }
            if($data['id']){
                //修改
                $choice = new mjudge();
                $result = $choice->save($data,['id' => $data['id']]);
                if($result){
                    $this->success('更新成功','Question/qlist');
                }else{
                    $this->error('更新失败');
                }
            }else{
                //新增
                unset($data['id']);
                $judge = new mjudge($data);
                if ($judge->save()) {
                    $this->success('添加成功','Index/index');
                }else{
                    $this->error('添加失败');
                }
            }
    	}else{
    		$this->error('请求不合法');
    	}
    }

    /**
     * 随机获取指定数量的填空题
     * @access public
     * @param $num 数量
     * @param $degree 难度
     * return string
     */
    public static function getJudge($num,$degree=null){
        $fillid = '';
        $idarr = self::getValueByColumn('id',$degree);
        //除了指定难度的试题的数量
        $idarrno = self::getValueByColumnRe('id',$degree);
        $idarrnum = count($idarr);//指定难度试题数量
        $bkey = count($idarrno)-1;
        if ($idarrnum > 0) {
            # 有该难度的试题 
            if ($num > $idarrnum) {
                # 难度试题不够
                $fillidarr = [];//把或者试题放进去 然后打乱顺序
                //符合难度的试题
                $allnum = $idarrnum-1;
                $allnumarr = range(0,$allnum);//利用range()函数产生一个数组
                shuffle($allnumarr);//利用shuffle()函数将产生的数组随机打乱顺序
                for ($i=0; $i < $idarrnum; $i++) {//选取数组
                    $fillidarr[] = $idarr[$allnumarr[$i]];
                }
                //补得不符合难度的试题
                $diff = $num - $idarrnum;//需要补充的试题数量 5-3=2
                $allnumarra = range(0,$bkey);//利用range()函数产生一个数组
                shuffle($allnumarra);//利用shuffle()函数将产生的数组随机打乱顺序
                for ($i=0; $i < $diff; $i++) {//选取数组
                    $index = $allnumarra[$i];
                    $fillidarr[] = $idarrno[$index];
                }
                
                shuffle($fillidarr);
                $fillid = implode(',',$fillidarr);
                
            }else{
                $allnum = $idarrnum-1;
                $allnumarr = range(0,$allnum);//利用range()函数产生一个数组
                shuffle($allnumarr);//利用shuffle()函数将产生的数组随机打乱顺序
                for ($i=0; $i < $num; $i++) {//选取数组前5个，即随机 
                    $fillid .= $idarr[$allnumarr[$i]].',';
                }
            }
        }else{
            //没有该难度的试题
            $allnumarr = range(0,$bkey);//利用range()函数产生一个数组
            shuffle($allnumarr);//利用shuffle()函数将产生的数组随机打乱顺序
            for ($i=0; $i < $num; $i++) {//选取数组
                $fillid .= $idarrno[$allnumarr[$i]].',';
            }
        }
        
        return trim($fillid,',');
    }
     /**
     * 查询指定类型的指定难度的试题的数量
     * @access public
     * @param $category 类型
     * @param $degree 试题难度
     * return string
     */
    public static function getCount($degree=null){
        $map = [];
        $map['creater'] = Session::get('id');
        if (!empty($degree)) {
            $map['degree'] = $degree;
        }
        $num = mjudge::where($map)->count();
        return $num;
    }
    /**
     * 查询指定类型的指定难度的试题的id值
     * @access public
     * @param $degree 试题难度
     * @param $column 列名
     * return string
     */
    public static function getValueByColumn($column,$degree=null){
        $map = [];
        $map['creater'] = Session::get('id');
        if (!empty($degree)) {
            $map['degree'] = $degree;
        }
        $idarr = mjudge::where($map)->column($column);
        return $idarr;
    }
    /**
     * 查询指定类型的不等于指定难度的试题的id值
     * @access public
     * @param $degree 试题难度
     * @param $column 列名
     * return string
     */
    public static function getValueByColumnRe($column,$degree){
        $map = [];
        $map['creater'] = Session::get('id');
        if (!empty($degree)) {
            $map['degree'] = ['<>',$degree];
        }
        $idarr = mjudge::where($map)->column($column);
        return $idarr;
    }
    /**
     * 预览
     * @access public
     * return array
     */
    public function preview(){
        if(Request::instance()->isGet()){
            $id = Request::instance()->param('id');
            $choice = mjudge::get($id);
            $this->assign("cdata",$choice);
            return $this->fetch();
        }else{
            $this->error('请求不合法');
        }
    }
    /**
     * 删除试题
     * @access public
     * return bool
     */
    public function delJudge(){
        if(Request::instance()->isGet()){
            $id = Request::instance()->param("id");
            if($id){
                $choice = mjudge::get($id);
                if($choice){
                    if($choice->paperid){
                        return $this->error("试题存在试卷,不能删除");
                    }else{
                        if(mjudge::destroy($id)){
                            return $this->success("删除成功",'Question/qlist');
                        }else{
                            return $this->error("删除失败");
                        }
                    }
                }else{
                    return $this->error("要删除的数据不存在");
                }
            }else{
                return $this->error("参数错误");
            }
        }else{
            return $this->error("请求方式错误");
        }
    }
    /**
     * 取出指定id的数据
     * @access public
     * @param string
     * return array
     */
    public static function getInfoById($ids){
        if($ids){
            $single = new mjudge();
            $data = $single->getInfoByIds($ids);
            return $data;
        }else{
            return false;
        }
    }
}
